package com.ushio.gougouwangguo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushio.commonmodule.pojo.user.User;
import com.ushio.commonmodule.utils.JwtUtil;
import com.ushio.gougouwangguo.entity.mysql.WeaponWarehouse;
import com.ushio.gougouwangguo.entity.qo.WeaponWarehouseListQo;
import com.ushio.gougouwangguo.entity.vo.WeaponLeafNodeVo;
import com.ushio.gougouwangguo.entity.vo.WeaponTreeNodeVo;
import com.ushio.gougouwangguo.entity.vo.WeaponWarehouseVo;
import com.ushio.gougouwangguo.mapper.CampMapper;
import com.ushio.gougouwangguo.service.CampService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 19451
 */
@Service
public class CampServiceImpl extends ServiceImpl<CampMapper,WeaponWarehouse> implements CampService  {

    @Autowired
    CampMapper campMapper;

    @Override
    public List<WeaponTreeNodeVo> weaponWarehouselSelect() {

        List<WeaponWarehouseVo> weaponWarehouseVos = campMapper.weaponWarehouselSelect(JwtUtil.parseUser().getUserId());

        Map<Long, WeaponTreeNodeVo> categoryMap = new LinkedHashMap<>();

        for (WeaponWarehouseVo weaponWarehouseVo:weaponWarehouseVos) {
            Long weaponWarehouseId = weaponWarehouseVo.getWeaponWarehouseId();
            String weaponName = weaponWarehouseVo.getWeaponName();
            Long weaponTypeId = weaponWarehouseVo.getWeaponTypeId();
            String weaponTypeName = weaponWarehouseVo.getWeaponTypeName();
            byte state = weaponWarehouseVo.getState();
            // 构建分类节点
            WeaponTreeNodeVo categoryNode = categoryMap.computeIfAbsent(weaponTypeId, id -> {
                WeaponTreeNodeVo node = new WeaponTreeNodeVo();
                node.setId(weaponTypeId);
                node.setLabel(weaponTypeName);
                return node;
            });
            // 装备可能为空（某分类下暂无装备）
            if (weaponWarehouseId != null && weaponName != null ) {
                WeaponLeafNodeVo leaf = new WeaponLeafNodeVo();
                leaf.setId(weaponWarehouseId);
                leaf.setLabel(weaponName);
                leaf.setState(state);
                categoryNode.getChildren().add(leaf);
            }

        }

       return new ArrayList<>(categoryMap.values());
    }

    @Override
    public List<WeaponWarehouseVo> weaponelSelect() {

        return campMapper.selectList(new LambdaQueryWrapper<WeaponWarehouse>().eq(WeaponWarehouse::getWeaponTypeId,1L).eq(WeaponWarehouse::getUserId,JwtUtil.parseUser().getUserId())).stream().map(weaponWarehouse -> {
            WeaponWarehouseVo weaponWarehouseVo = new WeaponWarehouseVo();
            BeanUtils.copyProperties(weaponWarehouse, weaponWarehouseVo);
            return weaponWarehouseVo;
        }).collect(Collectors.toList());

    }

    @Override
    public List<WeaponWarehouseVo> getProtectiveGearSelect() {
        return campMapper.selectList(new LambdaQueryWrapper<WeaponWarehouse>().eq(WeaponWarehouse::getWeaponTypeId,2L).eq(WeaponWarehouse::getUserId,JwtUtil.parseUser().getUserId())).stream().map(weaponWarehouse -> {
            WeaponWarehouseVo weaponWarehouseVo = new WeaponWarehouseVo();
            BeanUtils.copyProperties(weaponWarehouse, weaponWarehouseVo);
            return weaponWarehouseVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<WeaponWarehouseVo> getShoesGearSelect() {
        return campMapper.selectList(new LambdaQueryWrapper<WeaponWarehouse>().eq(WeaponWarehouse::getWeaponTypeId,3L).eq(WeaponWarehouse::getUserId,JwtUtil.parseUser().getUserId())).stream().map(weaponWarehouse -> {
            WeaponWarehouseVo weaponWarehouseVo = new WeaponWarehouseVo();
            BeanUtils.copyProperties(weaponWarehouse, weaponWarehouseVo);
            return weaponWarehouseVo;
        }).collect(Collectors.toList());
    }
}
